<template>
  <div class="trend-analysis charts-grid">
    <!-- 温湿度趋势图 -->
    <el-card class="chart-card">
      <template #header>
        <div class="card-header">
          <el-icon><TrendCharts /></el-icon>
          <span>温湿度变化趋势</span>
        </div>
      </template>
      <div class="chart-container">
        <div ref="tempHumidityChart" class="chart"></div>
      </div>
    </el-card>

    <!-- 土壤监测图 -->  
    <el-card class="chart-card">
      <template #header>
        <div class="card-header">
          <el-icon><TrendCharts /></el-icon>
          <span>土壤监测数据</span>
        </div>
      </template>
      <div class="chart-container">
        <div ref="soilChart" class="chart"></div>
      </div>
    </el-card>

    <!-- CO2浓度趋势图 -->
    <el-card class="chart-card">
      <template #header>
        <div class="card-header">
          <el-icon><TrendCharts /></el-icon>
          <span>CO2浓度变化趋势</span>
        </div>
      </template>
      <div class="chart-container">
        <div ref="co2Chart" class="chart"></div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import * as echarts from 'echarts'
import { TrendCharts } from '@element-plus/icons-vue'

const tempHumidityChart = ref()
const soilChart = ref()
const co2Chart = ref()

// 在script setup部分添加数据生成逻辑
const generateData = (startTime: string, hours: number = 24) => {
  const data = []
  const start = new Date(startTime)
  
  for(let i = 0; i < hours; i++) {
    const time = new Date(start.getTime() + i * 60 * 60 * 1000)
    data.push([
      time.toISOString(),
      {
        temp: 15 + Math.sin(i/6) * 3,  // 温度在12-18度之间波动
        humidity: 95 + Math.sin(i/8) * 5, // 湿度在90-100之间波动
        soilTemp: 15 + Math.sin(i/12) * 1, // 土壤温度在14-16度之间波动
        ph: Math.random() > 0.3 ? 7 : 3,  // 随机模拟PH值异常
        co2: 800 + Math.sin(i/4) * 100  // CO2在700-900之间波动
      }
    ])
  }
  return data
}

const mockData = generateData('2025-01-09 23:00:00')

// 初始化温湿度图表
const initTempHumidityChart = () => {
  const chart = echarts.init(tempHumidityChart.value)
  const option = {
    title: {
      text: '温湿度变化趋势',
      left: 'center',
      textStyle: {
        color: '#00e4ff'
      }
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      }
    },
    legend: {
      data: ['温度', '湿度'],
      bottom: '0%',
      textStyle: {
        color: '#7fdbff'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '10%',
      containLabel: true
    },
    xAxis: {
      type: 'time',
      show: true,
      boundaryGap: false,
      axisLine: {
        lineStyle: {
          color: '#7fdbff'
        }
      },
      axisLabel: {
        color: '#7fdbff',
        formatter: (value: string) => {
          const date = new Date(value)
          return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
        },
        interval: 4,
        rotate: 45,
        margin: 14
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: 'rgba(127,219,255,0.1)',
          type: 'dashed'
        }
      }
    },
    yAxis: [
      {
        type: 'value',
        name: '温度(°C)',
        position: 'left',
        axisLine: {
          lineStyle: {
            color: '#ffa940'
          }
        },
        axisLabel: {
          color: '#7fdbff'
        },
        splitLine: {
          show: false
        }
      },
      {
        type: 'value',
        name: '湿度(%)',
        position: 'right',
        axisLine: {
          lineStyle: {
            color: '#1890ff'
          }
        },
        axisLabel: {
          color: '#7fdbff'
        },
        splitLine: {
          show: false
        }
      }
    ],
    series: [
      {
        name: '温度',
        type: 'line',
        smooth: true,
        data: mockData.map(item => [item[0], item[1].temp]),
        itemStyle: {
          color: '#ffa940'
        }
      },
      {
        name: '湿度',
        type: 'line',
        smooth: true,
        yAxisIndex: 1,
        data: mockData.map(item => [item[0], item[1].humidity]),
        itemStyle: {
          color: '#1890ff'
        }
      }
    ]
  }
  chart.setOption(option)
}

// 初始化土壤监测图表
const initSoilChart = () => {
  const chart = echarts.init(soilChart.value)
  const option = {
    title: {
      text: '土壤监测数据',
      left: 'center',
      textStyle: {
        color: '#00e4ff'
      }
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['土壤温度', 'PH值'],
      bottom: '0%',
      textStyle: {
        color: '#7fdbff'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '10%',
      containLabel: true
    },
    xAxis: {
      type: 'time',
      show: true,
      axisLine: {
        lineStyle: {
          color: '#7fdbff'
        }
      },
      axisLabel: {
        color: '#7fdbff',
        formatter: (value: string) => {
          const date = new Date(value)
          return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
        },
        interval: 4,
        rotate: 45,
        margin: 14
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: 'rgba(127,219,255,0.1)',
          type: 'dashed'
        }
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        lineStyle: {
          color: '#7fdbff'
        }
      },
      axisLabel: {
        color: '#7fdbff'
      },
      splitLine: {
        show: false
      }
    },
    series: [
      {
        name: '土壤温度',
        type: 'bar',
        data: mockData.map(item => [item[0], item[1].soilTemp]),
        itemStyle: {
          color: '#52c41a'
        }
      },
      {
        name: 'PH值',
        type: 'bar',
        data: mockData.map(item => [item[0], item[1].ph]),
        itemStyle: {
          color: '#ff4d4f'
        },
        markLine: {
          data: [
            {
              name: 'PH警戒线',
              yAxis: 5,
              lineStyle: {
                color: '#ff4d4f'
              },
              label: {
                formatter: 'PH警戒线(5)',
                color: '#ff4d4f'
              }
            }
          ]
        }
      }
    ]
  }
  chart.setOption(option)
}

// 初始化CO2浓度图表
const initCO2Chart = () => {
  const chart = echarts.init(co2Chart.value)
  const option = {
    title: {
      text: 'CO2浓度变化趋势',
      left: 'center',
      textStyle: {
        color: '#00e4ff'
      }
    },
    tooltip: {
      trigger: 'axis'
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '10%',
      containLabel: true
    },
    xAxis: {
      type: 'time',
      show: true,
      boundaryGap: false,
      axisLine: {
        lineStyle: {
          color: '#7fdbff'
        }
      },
      axisLabel: {
        color: '#7fdbff',
        formatter: (value: string) => {
          const date = new Date(value)
          return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
        },
        interval: 4,
        rotate: 45,
        margin: 14
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: 'rgba(127,219,255,0.1)',
          type: 'dashed'
        }
      }
    },
    yAxis: {
      type: 'value',
      name: 'CO2浓度(ppm)',
      axisLine: {
        lineStyle: {
          color: '#7fdbff'
        }
      },
      axisLabel: {
        color: '#7fdbff'
      },
      splitLine: {
        show: false
      }
    },
    series: [
      {
        type: 'line',
        smooth: true,
        areaStyle: {
          opacity: 0.3,
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: '#9370DB'
            },
            {
              offset: 1,
              color: 'rgba(147,112,219,0.1)'
            }
          ])
        },
        data: mockData.map(item => [item[0], item[1].co2]),
        markLine: {
          data: [
            {
              name: '警戒线',
              yAxis: 1000,
              lineStyle: {
                color: '#FF0000'
              },
              label: {
                formatter: '警戒线(1000ppm)',
                color: '#ff4d4f'
              }
            }
          ]
        },
        itemStyle: {
          color: '#9370DB'
        }
      }
    ]
  }
  chart.setOption(option)
}

onMounted(() => {
  initTempHumidityChart()
  initSoilChart()
  initCO2Chart()
  
  // 监听窗口大小变化,重绘图表
  window.addEventListener('resize', () => {
    const charts = [
      tempHumidityChart.value && echarts.getInstanceByDom(tempHumidityChart.value),
      soilChart.value && echarts.getInstanceByDom(soilChart.value),
      co2Chart.value && echarts.getInstanceByDom(co2Chart.value)
    ]
    charts.forEach(chart => chart?.resize())
  })
})
</script>

<style scoped lang="scss">
.trend-analysis {
  // 网格布局
  &.charts-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    padding: 20px;
  }

  .chart-card {
    background: rgba(0,0,0,0.1);
    border: 1px solid rgba(0,255,255,0.1);
    
    :deep(.el-card__header) {
      border-bottom: 1px solid rgba(0,255,255,0.1);
      padding: 10px 20px;
    }
  }

  .card-header {
    display: flex;
    align-items: center;
    gap: 8px;
    color: #00e4ff;
  }

  .chart-container {
    padding: 20px;
    
    .chart {
      height: 300px;
      width: 100%;
    }
  }
}
</style>